package cn.hyxy.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class Demo01_DefaultMR extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        if(args.length!=2){
            System.out.println("usage :in out");
            return -1;
        }
        Path src = new Path(args[0]);//输入目录
        Path desc = new Path(args[1]);//输出的目录
        Configuration config = getConf();
        FileSystem fs = FileSystem.get(config);
        if(fs.exists(desc)){
            fs.delete(desc,true);
        }
        //声明Job
        Job job = Job.getInstance(config);
        job.setJobName("DefaultMR2");
        job.setJarByClass(getClass());
        job.setNumReduceTasks(0);
        FileInputFormat.addInputPath(job,src);
        FileOutputFormat.setOutputPath(job,desc);
        boolean boo = job.waitForCompletion(true);
        return boo?0:1;
    }
    //hadoop jar Demo01_DefaultMR /test/   /out001
    public static void main(String[] args) throws Exception {
        int code = ToolRunner.run(new Demo01_DefaultMR(),args);
        System.exit(code);
    }
}
